//
//  GenericController.m
//  iCodeMac
//
//  Created by Andrew Barry on 28/02/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "GenericController.h"
#import "FormEditor.h"
#import "ControlPanel.h"
#import "JavascriptForm.h"

@implementation GenericController

-(id) init {
	controls = [[NSMutableArray alloc] init];
	properties = [[NSMutableArray alloc] init];
	controlTypes = [[NSMutableArray alloc] init];
	return [super init];
}

-(void) bindCheckbox: (NSButton *) check toProperty: (NSString *) name {
	[controls addObject:check];
	[controlTypes addObject:@"check"];
	[properties addObject: name];
	
	[check setState: [[ControlPanel valueForProperty: name] intValue]];
	[check setTarget: self];
	[check setAction: @selector(checkChanged:)];
}

-(void) bindColorWell: (NSColorWell *) field toProperty: (NSString *) name {
	[controls addObject:field];
	[controlTypes addObject:@"color"];
	[properties addObject: name];
	
	NSColor *col = [ControlPanel valueForProperty: name];
	if (col)
		[field setColor:col];
	[field setTarget: self];
	[field setAction: @selector(colorChanged:)];
}

-(void) bindText: (NSTextField *) field toProperty: (NSString *) name {	
	[controls addObject:field];
	[controlTypes addObject:@"editString"];
	[properties addObject: name];
	
	[field setDelegate: self];
	NSString *val = [ControlPanel valueForProperty: name];
	[field setStringValue:val ? val : @""];
}

-(void) bindInt: (NSTextField *) field toProperty: (NSString *) name {	
	[controls addObject:field];
	[controlTypes addObject:@"editInt"];
	[properties addObject: name];
	
	[field setDelegate: self];
	[field setStringValue:[[ControlPanel valueForProperty: name] stringValue]];
}

-(void) bindPopup: (NSPopUpButton *) field toProperty: (NSString *) name {	
	[controls addObject:field];
	[controlTypes addObject:@"popupInt"];
	[properties addObject: name];
	
	[field setAction: @selector(popupAction:)];
	[field setTarget:self];
	
	int v = [[ControlPanel valueForProperty: name] intValue];
	
	[field selectItem:[[field menu] itemAtIndex:v]];
}

-(void) bindDataSource: (NSPopUpButton *) field toProperty: (NSString *) name {
	[controls addObject:field];
	[controlTypes addObject:@"popupDataSource"];
	[properties addObject: name];

	FormControl *currentDataSource = [ControlPanel valueForProperty: name];
	
	[field addItemWithTitle:@"None"];
	int selIndex = 0;
	// add any data source controls that are on the same form
	JavascriptForm *form = [ControlPanel form];
	FormEditor *editor = [form editor];
	NSArray *formControls = [editor controls];
	for (int idx = 0; idx < [formControls count]; idx++) {
		FormControl *ctl = [formControls objectAtIndex:idx];
		if ([[ctl typeName] isEqualToString:@"data"]) {
			if (ctl == currentDataSource)
				selIndex = [field numberOfItems];
			[field addItemWithTitle:[ctl controlName]];
		}
	}
	
	[field setAction: @selector(popupAction:)];
	[field setTarget:self];

	[field selectItemAtIndex: selIndex];
}

- (void)controlTextDidChange:(NSNotification *)aNotification {
	int idx;
	NSTextField *sender = [aNotification object];
	
	for (idx = 0; idx < [controls count]; idx++) {
		if (sender == [controls objectAtIndex:idx]) {
			NSString *controlType = [controlTypes objectAtIndex: idx];
			NSString *prop = [properties objectAtIndex:idx];
			if ([controlType isEqualToString:@"editString"]) {
				[ControlPanel setValue: [sender stringValue] forProperty: prop];
			} else if ([controlType isEqualToString:@"editInt"]) {
				[ControlPanel setValue: [NSNumber numberWithInt:[sender intValue]] forProperty: prop];
			}
		}
	}
}

-(void)popupAction: (NSPopUpButton *) sender {
	int idx;
	int v = [[sender menu] indexOfItem:[sender selectedItem]];
	
	for (idx = 0; idx < [controls count]; idx++) {
		if (sender == [controls objectAtIndex:idx]) {
			NSString *controlType = [controlTypes objectAtIndex: idx];
			NSString *prop = [properties objectAtIndex:idx];
			if ([controlType isEqualToString:@"popupInt"]) {
				[ControlPanel setValue: [NSNumber numberWithInt:v] forProperty: prop];
			} else if ([controlType isEqualToString:@"popupDataSource"]) {
				NSString *name = [[sender selectedItem] title];
				FormEditor *editor = [[ControlPanel form] editor];
				FormControl *dataSource = nil;
				if (v > 0)
					dataSource = [editor controlByName: name];
				[ControlPanel setValue: dataSource ? dataSource : [NSNull null] forProperty: prop];

			}
		}
	}
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification {
	int idx;
	NSTextField *sender = [aNotification object];
	
	for (idx = 0; idx < [controls count]; idx++) {
		if (sender == [controls objectAtIndex:idx]) {
			NSString *controlType = [controlTypes objectAtIndex: idx];
			NSString *prop = [properties objectAtIndex:idx];
			if ([controlType isEqualToString:@"editString"]) {
				[ControlPanel setValue: [sender stringValue] forProperty: prop];
			} else if ([controlType isEqualToString:@"editInt"]) {
				[ControlPanel setValue: [NSNumber numberWithInt:[sender intValue]] forProperty: prop];
			}
		}
	}
}

-(void) checkChanged: (id) sender {
	int idx;
	for (idx = 0; idx < [controls count]; idx++) {
		if (sender == [controls objectAtIndex:idx]) {
			NSString *prop = [properties objectAtIndex:idx];
			[ControlPanel setValue: [NSNumber numberWithInt:[sender state]] forProperty: prop];
		}
	}
}

-(void) colorChanged: (id) sender {
	int idx;
	for (idx = 0; idx < [controls count]; idx++) {
		if (sender == [controls objectAtIndex:idx]) {
			NSString *prop = [properties objectAtIndex:idx];
			[ControlPanel setValue: [sender color] forProperty: prop];
		}
	}
}

-(void) updateValue: (id) val forProperty: (NSString*) key {
	int idx;
	for (idx = 0; idx < [controls count]; idx++) {
		if ([key isEqualToString: [properties objectAtIndex:idx]]) {
			if ([@"check" isEqualToString:[controlTypes objectAtIndex:idx]]) {
				NSButton * check = [controls objectAtIndex:idx];
				[check setState: [val intValue]];
			} else if ([@"editString" isEqualToString:[controlTypes objectAtIndex:idx]]) {
				NSTextField * field = [controls objectAtIndex:idx];
				[field setStringValue:val];
			} else if ([@"editInt" isEqualToString:[controlTypes objectAtIndex:idx]]) {
				NSTextField * field = [controls objectAtIndex:idx];
				[field setStringValue:[val stringValue]];
			}
			return;
		}
	}
}

@end
